package networkgui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.mail.MessagingException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import networkbackend.EmailClass;

/**
 * 
 * Email form for sending email.
 * 
 */
public class EmailForm extends javax.swing.JFrame {

	/**
	 * Variables. Woo!
	 */
	private static final long serialVersionUID = 1L;
	/** Variables Declaration * */
	private javax.swing.JButton btnSend;
	private javax.swing.JCheckBox cbAuthenticated;
	private javax.swing.JPasswordField txtPassword;
	private javax.swing.JScrollPane spRecipients;
	private javax.swing.JScrollPane spBody;
	private javax.swing.JTextArea taRecipients;
	private javax.swing.JTextArea taBody;
	private javax.swing.JTextField txtSMTP;
	private javax.swing.JTextField txtFrom;
	private javax.swing.JTextField txtUserName;
	private javax.swing.JTextField txtSubject;
	private javax.swing.JLabel lblSMTP;
	private javax.swing.JLabel lblFrom;
	private javax.swing.JLabel lblRecipients;
	private javax.swing.JLabel lblSubject;
	private javax.swing.JLabel lblUserName;
	private javax.swing.JLabel lblPassword;
	private JPanel MyPanel = new JPanel();
	private JPanel AuthenticationPanel = new JPanel();
	private JPanel EmailPanel = new JPanel();
	private boolean Authenticated = false;

	/** End of variables declaration* */

	// constructor
	public EmailForm() {
		// initiate all controls
		initComponents();
		this.setTitle("Sendin' Emailz");
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

	}

	private void initComponents() {

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		// Initialize containers/////////////////////////////
		spBody = new javax.swing.JScrollPane();
		taBody = new javax.swing.JTextArea();
		lblSubject = new javax.swing.JLabel();
		lblRecipients = new javax.swing.JLabel();
		txtUserName = new javax.swing.JTextField();
		txtSMTP = new javax.swing.JTextField();
		lblSMTP = new javax.swing.JLabel();
		lblFrom = new javax.swing.JLabel();
		spRecipients = new javax.swing.JScrollPane();
		taRecipients = new javax.swing.JTextArea();
		txtFrom = new javax.swing.JTextField();
		txtSubject = new javax.swing.JTextField();
		lblUserName = new javax.swing.JLabel();
		cbAuthenticated = new javax.swing.JCheckBox();
		lblPassword = new javax.swing.JLabel();
		txtPassword = new javax.swing.JPasswordField();
		btnSend = new javax.swing.JButton();
		// end initialize containers////////////////////////

		// Set Properties for containers
		spBody.setViewportView(taBody);
		lblSubject.setFont(new java.awt.Font("Tahoma", 1, 12));
		lblSubject.setText("Subject:");
		lblRecipients.setFont(new java.awt.Font("Tahoma", 1, 12));
		lblRecipients.setText("Recipients:");
		lblSMTP.setFont(new java.awt.Font("Tahoma", 1, 12));
		lblSMTP.setText("SMTP:");
		lblFrom.setFont(new java.awt.Font("Tahoma", 1, 12));
		lblFrom.setText("From:");
		taRecipients.setColumns(20);
		taRecipients.setRows(5);
		spRecipients.setViewportView(taRecipients);
		lblUserName.setFont(new java.awt.Font("Tahoma", 1, 12));
		lblUserName.setText("User Name:");
		cbAuthenticated.setText("Authenticated");
		cbAuthenticated.setMargin(new java.awt.Insets(0, 0, 0, 0));

		lblPassword.setFont(new java.awt.Font("Tahoma", 1, 12));
		lblPassword.setText("Password:");
		btnSend.setText("Send Message");

		txtFrom.setPreferredSize(new Dimension(200, 19));
		spBody.setPreferredSize(new Dimension(500, 200));
		txtUserName.setPreferredSize(new Dimension(11, 19));
		txtSMTP.setPreferredSize(new Dimension(200, 19));
		spRecipients.setPreferredSize(new Dimension(430, 30));
		txtSubject.setPreferredSize(new Dimension(200, 19));
		// cbAuthenticated.setPreferredSize(new Dimension(250, 50));
		txtPassword.setPreferredSize(new Dimension(200, 19));
		btnSend.setPreferredSize(new Dimension(250, 50));
		// End Set Properties for containers

		// set up events
		cbAuthenticated.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				cbAuthenticatedStateChanged(evt);
			}
		});

		btnSend.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				try {
					OnbtnSendClick(evt);
				} catch (MessagingException ex) {

					ex.printStackTrace();

				}
			}
		});

		Container VerticalBox = Box.createVerticalBox();
		Container VerticalBox2 = Box.createVerticalBox();
		Container VerticalBox3 = Box.createVerticalBox();

		Border etched = BorderFactory.createEtchedBorder();

		VerticalBox2.add(cbAuthenticated);
		VerticalBox2.add(lblUserName);
		VerticalBox2.add(txtUserName);
		VerticalBox2.add(lblPassword);
		VerticalBox2.add(txtPassword);

		lblUserName.setEnabled(false);
		txtUserName.setEnabled(false);
		lblPassword.setEnabled(false);
		txtPassword.setEnabled(false);

		VerticalBox2.add(new JLabel("          "));
		AuthenticationPanel.add(VerticalBox2);
		AuthenticationPanel.setBorder(BorderFactory.createTitledBorder(etched,
				""));

		VerticalBox.add(AuthenticationPanel);
		VerticalBox.add(lblSMTP);
		VerticalBox.add(txtSMTP);
		VerticalBox.add(lblFrom);
		VerticalBox.add(txtFrom);
		VerticalBox.add(lblRecipients);
		VerticalBox.add(spRecipients);
		VerticalBox.add(lblSubject);
		VerticalBox.add(txtSubject);
		VerticalBox.add(new JLabel("         "));
		VerticalBox.add(spBody);
		VerticalBox.add(new JLabel("          "));
		VerticalBox.add(btnSend);
		EmailPanel.add(VerticalBox);
		EmailPanel.setBorder(BorderFactory.createTitledBorder(etched, ""));

		// add box layout to panel
		VerticalBox3.add(AuthenticationPanel);
		VerticalBox3.add(EmailPanel);

		MyPanel.add(VerticalBox3);

		// add panel to the frame's contentpane
		this.setContentPane(MyPanel);

		this.pack();

	}

	// Checkbox handler
	private void cbAuthenticatedStateChanged(java.awt.event.ItemEvent evt) {
		if (cbAuthenticated.isSelected()) {

			lblUserName.setEnabled(true);
			txtUserName.setEnabled(true);
			lblPassword.setEnabled(true);
			txtPassword.setEnabled(true);
			Authenticated = true;

		} else {

			lblUserName.setEnabled(false);
			txtUserName.setEnabled(false);
			lblPassword.setEnabled(false);
			txtPassword.setEnabled(false);
			Authenticated = false;
		}
	}

	// button click handler
	private void OnbtnSendClick(MouseEvent evt) throws MessagingException {

		String tempstring = taRecipients.getText();
		String[] recipients = tempstring.split(",");

		EmailClass EmailSender = new EmailClass();

		// bool to tell us if the email actually sent
		boolean emailSent = false;

		// bool to tell if the fields being used in the email are valid (ie, not
		// empty)
		boolean fieldsOK = true;

		// it would appear as though we don't actually *do* anything here ...
		// if the checkbox is checked
		if (Authenticated) {
			// EmailSender.SendAuthenticatedMessage(recipients,
			// txtSubject.getText(), taBody.getText(), txtFrom.getText(),
			// txtSMTP.getText(), txtUserName.getText(),
			// txtPassword.getPassword());
		} else {
			// EmailSender.SendMessage(recipients, txtSubject.getText(),
			// taBody.getText(), txtFrom.getText(), txtSMTP.getText());
		}

		// checkin' da email fieldz
		if (recipients == null || recipients.length < 2) {
			JOptionPane.showMessageDialog(null, "Please enter recipients",
					"No Recipients", JOptionPane.ERROR_MESSAGE);
			fieldsOK = false;
		}
		if (txtSubject.getText().trim() == null
				|| txtSubject.getText().trim() == "") {
			JOptionPane.showMessageDialog(null, "Please enter a subject",
					"No Subject", JOptionPane.ERROR_MESSAGE);
			fieldsOK = false;
		}
		if (taBody.getText().trim() == null || taBody.getText().trim() == "") {
			JOptionPane.showMessageDialog(null,
					"Please enter text in the body", "No body text",
					JOptionPane.ERROR_MESSAGE);
			fieldsOK = false;
		}
		if (txtFrom.getText().trim() == null || txtFrom.getText().trim() == "") {
			JOptionPane.showMessageDialog(null,
					"Please enter text in the 'from' field", "No from text",
					JOptionPane.ERROR_MESSAGE);
			fieldsOK = false;
		}
		if (txtSMTP.getText().trim() == null || txtSMTP.getText().trim() == "") {
			JOptionPane.showMessageDialog(null,
					"Please enter SMTP information", "No SMTP",
					JOptionPane.ERROR_MESSAGE);
			fieldsOK = false;
		}
		if (fieldsOK) {
			try {
				EmailSender.SendMessage(recipients, txtSubject.getText(),
						taBody.getText(), txtFrom.getText(), txtSMTP.getText());
				emailSent = true;
			} catch (MessagingException ex) {
				emailSent = false;
				ex.printStackTrace();
				throw ex;
			} finally {
				System.out.println("Email Sent Successfully: " + emailSent);
			}
		}
	}
}
